let express = require("express")
let login_router = express.Router()
let sqlQuery = require("../mysql/mysql");
// 处理post请求的中间件，用于解析post返回的数据
const bodyParser = require('body-parser')
login_router.use(bodyParser.urlencoded({extended:false}))

// 超级管理员登录接口
login_router.post('/',async(req,res) => {
  let admin = req.body.username
  let password = req.body.password;
  console.log(admin,password)
  
  let sqlStr = "select * from onepice_root where user = ? and password = ?";
  let results = await sqlQuery(sqlStr,[admin,password]);
  //判断results有无数据来决定登录成功与否
  if(results.length!=0){
    console.log("66");
  return res.json({ status: 1, msg: '登录成功',user:'管理员' })
  }else{
   return res.json({ status: 2, msg: '登录失败,账号或密码错误！' })
  }
 
  let options = {
    
  }
})

//普通员工登录接口
login_router.post('/user',async(req,res) => {
  let id = req.body.username
  let password = req.body.password;
  // let password = req.query.password;
  // console.log(staffId,password)
  let sqlStr = "select * from onepice_user where id = ? and password = ?";
  let results = await sqlQuery(sqlStr,[id,password]);
  // let results = await sqlQuery(sqlStr);
  //判断results有无数据来决定登录成功与否
  if(results.length!=0){
    console.log("login接口信息开始*****************")
    console.log(results)
    console.log("login接口信息结束*****************")
  return res.json({ results,status: 1, msg: '登录成功' })
  }else{
   return res.json({ status: 2, msg: '登录失败,账号或密码错误！' })
  }
 
  let options = {
    
  }
})

module.exports = login_router